package com.utils
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	
	/**
	 * 键控代码内存储器 用来存储每个按键按下或松开的状态
	 * 提供检查某个按键是否按下的功能
	 */		
	public class KeyBoardUtil
	{
		private static var keyboardStore:Object = {};
		private static var stage:Stage;

		private static var _callBack:Function = new Function();
		/**传入舞台实例 监听键盘按下和松开的事件*/		
		public static function registerStage(dpc:DisplayObjectContainer):void{
			stage = dpc.stage;
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
		}
		
		/**存储对应按下的键控代码状态为true*/		
		private static function keyDown(e:KeyboardEvent):void
		{
			keyboardStore[e.keyCode] = true;
			_callBack();
		}	
		
		/**存储对应按下的键控代码状态为false*/		
		private static function keyUp(e:KeyboardEvent):void
		{
			keyboardStore[e.keyCode] = false;
			_callBack();
		}
		
		/**
		 * 检查某个键控代码对应的按键当前是否按下了
		 * @param keyCode 目标键控代码
		 * @param onceResponseState 键盘按键按下是否单次响应 true 按下不松开只响应一次
		 */		
		public static function keyIsDown(keyCode:int):Boolean{
			return keyboardStore[keyCode]; 
		}
		
		
		/**
		 *移除键盘侦听器 
		 */		
		public static function removeListen():void{
			keyboardStore = {};
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
		}
		
		public static function set callBack(value:Function):void
		{
			_callBack = value;
		}
		
	}
}